package com.cloud.point.concurrent.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;

/**
 * CompletableFuture 并发编程
 *
 * @author 24373
 * @version 1.0
 * @date 2024-07-16 23:21
 */
@Slf4j
public class TestCompletableFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        StopWatch watch = new StopWatch();
        watch.start();
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "name";
        });
        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "id";
        });
        try {
            Thread.sleep(600);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        watch.stop();
        String name = f1.get();
        log.info("name:{}", name);
        String id = f2.get();
        log.info("id:{}", id);
        log.info("耗时：{}", watch.getTotalTimeMillis());

    }

}
